//
//  TouchFirstView.m
//  A段项目
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "TouchFirstView.h"
#import "Model.h"
#import "SXLineLayout.h"
#import "TouchViewCell.h"
#import "UMSocial.h"
#import <MBProgressHUD.h>
#import <UIImageView+WebCache.h>
#import "SXStackLayout.h"


@interface TouchFirstView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrModel;

@property (nonatomic, retain) NSMutableArray *arrPic;

@property (nonatomic, retain) UIView *visualView;

@property (nonatomic,retain) UIImageView *backImage;

@end

@implementation TouchFirstView

- (void)dealloc {
    
    [_arrPic release];
    [_collectionView release];
    [_arrModel release];
    [_array release];
    
    [_backImage release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self dataArray];
    
    [self handleData];
    
    self.backImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:self.backImage];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:2];
    self.visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
    self.visualView.frame = self.view.bounds;
    
    self.visualView.alpha = 0.6;
    
    [self.view addSubview:self.visualView];
    [_visualView release];
    
    
    
    [self createCollectionView];
    
  
}

#pragma mark -- 保存状态


- (NSArray *)dataArray {
    
    if (self.arrModel == nil) {
        
        self.arrModel = [NSMutableArray array];
        
        return self.arrModel;
        
    }else {
        
        
        return self.arrModel;
    }
    
}

- (void)handleData {

    
    for (NSDictionary *dic in self.array) {
        
        Model *modle = [[Model alloc] init];
    
        [modle setValuesForKeysWithDictionary:dic];
        
        [self.arrModel addObject:modle];
        
        [modle release];
        
    }
    
  
    
}

/**创建 collectionview. */

- (void)createCollectionView {
    
    SXLineLayout *layout= [[SXLineLayout alloc] init];
  //  SXStackLayout *layout = [[SXStackLayout alloc] init];
    
    CGFloat itemWH = self.view.frame.size.height * 0.5;
    

     layout.itemSize = CGSizeMake(itemWH, itemWH * 1.8);
    
   
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    
    [self.view addSubview:self.collectionView];
    
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    
    [self.collectionView registerClass:[TouchViewCell class] forCellWithReuseIdentifier:@"pool1"];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"pool2"];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    [self.collectionView addGestureRecognizer:tap];
    
    [layout release];
    
    
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
   
    return self.arrModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    Model *model = [self.arrModel objectAtIndex:indexPath.item];
    
    TouchViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    cell.imageViewForTouch.transform = CGAffineTransformIdentity;

    cell.backgroundColor = [UIColor clearColor];

    [cell passedInfoImage:model];
    
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:model.url]];
    
    return cell;
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    

    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
